package com.zhiyu.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.zhiyu.api.ReturnMsg;
import com.zhiyu.api.ReturnResult;
import com.zhiyu.dao.ISysUserMapper;
import com.zhiyu.entity.SysUser;
import com.zhiyu.service.webApi.ISysUserService;
import com.zhiyu.utils.DataUtil;
import com.zhiyu.utils.JwtUtil;
import com.zhiyu.utils.UuidUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.RowBounds;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service(version = "1.0.0")
@Slf4j
public class SysUserServiceImpl implements ISysUserService {

    @Resource
    private ISysUserMapper iSysUserMapper;

    /**
     * 获取所有未冻结的系统用户信息列表
     * @param param
     * @return
     */
    @Override
    public ReturnResult getAllUnfrozenSysUserList(Map<String, Object> param) {
        ReturnResult returnResult = ReturnResult.success();
        try {
            // 获取所有用户信息
            List<SysUser> list = iSysUserMapper.getAllUnfrozenSysUserList();
            returnResult.setData(list);
            returnResult.setTotal(list.size());
        } catch (Exception e) {
            log.error("获取所有未冻结的系统用户信息列表发生异常",e);
            returnResult.setCode(ReturnMsg.ERROR.getCode());
            returnResult.setMsg("加载未冻结的系统用户信息列表发生异常");
            return returnResult;
        }
        return returnResult;
    }

    /**
     * 根据查询条件查询系统用户信息列表
     * @param param
     * @return
     */
    @Override
    public ReturnResult getSysUserList(Map<String, Object> param) {
        ReturnResult returnResult = ReturnResult.success();
        try {
            // 每页显示多少条
            int pageNum = DataUtil.asInt(param.get("limit"), 50);
            // 查询用户列表总数
            Integer count = iSysUserMapper.getSysUserListCount((Map)param.get("conditions"));
            // 查询用户列表信息
            List<SysUser> list = iSysUserMapper.getSysUserList((Map)param.get("conditions"), new RowBounds((DataUtil.asInt(param.get("page"), 1)-1) * pageNum, pageNum));
            returnResult.setData(list);
            returnResult.setTotal(count);
        } catch (Exception e) {
            log.error("根据查询条件查询系统用户信息列表发生异常",e);
            returnResult.setCode(ReturnMsg.ERROR.getCode());
            returnResult.setMsg("查询用户列表发生异常");
            return returnResult;
        }
        return returnResult;
    }

    /**
     * 根据token查询用户
     * @param param
     * @return
     */
    @Override
    public ReturnResult getSysUserByToken(Map<String, Object> param) {
        ReturnResult returnResult = ReturnResult.success();
        try {
            // token校验
            String userId = JwtUtil.verity(DataUtil.asString(param.get("token")));
            param.put("userId", userId);
            SysUser sysUser = iSysUserMapper.getSysUserById(param);
            returnResult.setData(sysUser);
        } catch (Exception e) {
            log.error("登录发生异常",e);
            returnResult.setCode(ReturnMsg.ERROR.getCode());
            returnResult.setMsg("登录发生异常");
            return returnResult;
        }
        return returnResult;
    }

    /**
     * 根据id查询系统用户
     * @param param
     * @return
     */
    @Override
    public ReturnResult getSysUserById(Map<String, Object> param) {
        ReturnResult returnResult = ReturnResult.success();
        try {
            SysUser sysUser = iSysUserMapper.getSysUserById(param);
            returnResult.setData(sysUser);
        } catch (Exception e) {
            log.error("登录发生异常",e);
            returnResult.setCode(ReturnMsg.ERROR.getCode());
            returnResult.setMsg("登录发生异常");
            return returnResult;
        }
        return returnResult;
    }

    /**
     * 校验用户名是否已存在
     * @param param
     * @return
     */
    @Override
    public ReturnResult checkIsExistSysUserByUserName(Map<String, Object> param) {
        ReturnResult returnResult = ReturnResult.success();
        try {
            // 验证用户名是否已存在
            Integer num = iSysUserMapper.checkIsExistSysUserByUserName(param);
            returnResult.setData(num);
        } catch (Exception e) {
            log.error("校验用户名是否已存在发生异常",e);
            returnResult.setCode(ReturnMsg.ERROR.getCode());
            returnResult.setMsg("校验用户名是否已存在发生异常");
            return returnResult;
        }
        return returnResult;
    }

    /**
     * 新增系统用户
     * @param param
     * @return
     */
    @Override
    @Transactional
    public ReturnResult addSysUser(Map<String, Object> param) {
        ReturnResult returnResult = ReturnResult.success();
        try {
            // 根据用户id查询用户信息
            SysUser sysUser = iSysUserMapper.getSysUserById(param);
            // 获取主键
            String uuid = UuidUtil.getUUID();
            param.put("userId",uuid);
            param.put("createUserId",sysUser.getId());
            param.put("createUserName",sysUser.getUserName());
            // 新增用户
            iSysUserMapper.addSysUser(param);
        }catch (Exception e){
            log.error("新增系统用户发生异常",e);
            returnResult.setCode(ReturnMsg.ERROR.getCode());
            returnResult.setMsg("新增系统用户发生异常");
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return returnResult;
        }
        return returnResult;
    }

    /**
     * 修改系统用户
     * @param param
     * @return
     */
    @Override
    public ReturnResult updateSysUser(Map<String, Object> param) {
        ReturnResult returnResult = ReturnResult.success();
        try {
            // 根据用户id查询用户信息
            SysUser sysUser = iSysUserMapper.getSysUserById(param);
            // 修改用户
            param.put("updateUserId",sysUser.getId());
            param.put("updateUserName",sysUser.getUserName());
            iSysUserMapper.updateSysUser(param);
        } catch (Exception e) {
            log.error("修改系统用户发生异常",e);
            returnResult.setCode(ReturnMsg.ERROR.getCode());
            returnResult.setMsg("修改系统用户发生异常");
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return returnResult;
        }
        return returnResult;
    }

    /**
     * 修改密码
     * @param param
     * @return
     */
    @Override
    @Transactional
    public ReturnResult updatePassword(Map<String, Object> param) {
        ReturnResult returnResult = ReturnResult.success();
        try {
            // 根据用户id查询用户信息
            SysUser sysUser = iSysUserMapper.getSysUserById(param);
            if (sysUser.getPassword().equals(param.get("password"))) {
                // 重置密码
                param.put("id",sysUser.getId());
                param.put("updateUserId",sysUser.getId());
                param.put("updateUserName",sysUser.getUserName());
                iSysUserMapper.updatePassword(param);
            } else {
                returnResult.setCode(ReturnMsg.ERROR.getCode());
                returnResult.setMsg("原密码不正确，请重新输入");
            }
        } catch (Exception e) {
            log.error("修改密码发生异常",e);
            returnResult.setCode(ReturnMsg.ERROR.getCode());
            returnResult.setMsg("修改密码发生异常");
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return returnResult;
        }
        return returnResult;
    }

    /**
     * 重置密码
     * @param param
     * @return
     */
    @Override
    @Transactional
    public ReturnResult resetPassword(Map<String, Object> param) {
        ReturnResult returnResult = ReturnResult.success();
        try {
            // 根据用户id查询用户信息
            param.put("userId",param.get("id"));
            SysUser sysUser = iSysUserMapper.getSysUserById(param);
            // 重置密码
            param.put("updateUserId",sysUser.getId());
            param.put("updateUserName",sysUser.getUserName());
            iSysUserMapper.resetPassword(param);
        } catch (Exception e) {
            log.error("重置密码发生异常",e);
            returnResult.setCode(ReturnMsg.ERROR.getCode());
            returnResult.setMsg("重置密码发生异常");
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return returnResult;
        }
        return returnResult;
    }

    /**
     * 删除系统用户
     * @param param
     * @return
     */
    @Override
    @Transactional
    public ReturnResult deleteSysUser(Map<String, Object> param) {
        ReturnResult returnResult = ReturnResult.success();
        try {
            // 删除用户
            iSysUserMapper.deleteSysUser(param);
        } catch (Exception e) {
            log.error("删除系统用户发生异常",e);
            returnResult.setCode(ReturnMsg.ERROR.getCode());
            returnResult.setMsg("删除系统用户发生异常");
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return returnResult;
        }
        return returnResult;
    }

    /**
     * 冻结、解冻系统用户
     * @param param
     * @return
     */
    @Override
    @Transactional
    public ReturnResult frozenSysUser(Map<String, Object> param) {
        ReturnResult returnResult = ReturnResult.success();
        try {
            // 冻结、解冻用户
            iSysUserMapper.frozenSysUser(param);
        } catch (Exception e) {
            log.error("冻结、解冻系统用户发生异常",e);
            returnResult.setCode(ReturnMsg.ERROR.getCode());
            returnResult.setMsg("冻结、解冻系统用户发生异常");
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return returnResult;
        }
        return returnResult;
    }

}
